package com.bawei.shoppingcar;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bawei.shoppingcar.entity.GoodsEntity;
import com.bawei.shoppingcar.entity.ResultEntity;
import com.bawei.shoppingcar.utils.HttpUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;

public class GoodsDetailActivity extends AppCompatActivity {
    private ImageView ivIcon;
    private TextView tvDesc;
    private TextView tvPrice;
    private ImageView ivOne;
    private ImageView ivTwo;
    private ImageView ivGocar;
    private TextView tvAddcar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_detail);
        ivIcon = (ImageView) findViewById(R.id.iv_icon);
        tvDesc = (TextView) findViewById(R.id.tv_desc);
        tvPrice = (TextView) findViewById(R.id.tv_price);
        ivOne = (ImageView) findViewById(R.id.iv_one);
        ivTwo = (ImageView) findViewById(R.id.iv_two);
        ivGocar = (ImageView) findViewById(R.id.iv_gocar);
        tvAddcar = (TextView) findViewById(R.id.tv_addcar);
        //接受传递的数据
        GoodsEntity.DataBean dataBean = (GoodsEntity.DataBean) getIntent().getExtras().getSerializable("goods");
        //展示数据
        Glide.with(this).load(dataBean.getGoods_icon()).into(ivIcon);
        tvDesc.setText(dataBean.getGoods_desc()+"");
        tvPrice.setText("原价¥："+dataBean.getGoods_price());
        Glide.with(this).load(dataBean.getGoods_detail_one()).into(ivOne);
        Glide.with(this).load(dataBean.getGoods_detail_two()).into(ivTwo);
        //点击添加购物车
        tvAddcar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AddCarTask().execute("http://43.143.146.165:8083/car/addCar?goods_id="+dataBean.getId()+"&username=zhoukun");
            }
        });
        //点击购物车跳转到购物车页面
        ivGocar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(GoodsDetailActivity.this, CarActivity.class);
                startActivity(intent);
            }
        });
    }

    class AddCarTask extends AsyncTask<String,Void,String> {

        @Override
        protected String doInBackground(String... strings) {
            return HttpUtils.doGet(strings[0]);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            ResultEntity resultEntity = new Gson().fromJson(s, ResultEntity.class);
            if(resultEntity.getCode() == 200){
                Toast.makeText(GoodsDetailActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(GoodsDetailActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

}